package org.mysterylab.utopiaframework.web.controller;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 定义标准的 REST 方法来实现对实体对象的操作, 达到统一 REST 方法的方法名称目的.
 * 这样还可以避免子类需要重复编写"@RequestMapping". 子类要实现某功能只需覆盖下面的方法即可,
 * 在覆盖时注意加上 "@Override"
 * 
 * <pre>
 * /userinfo        GET     -> show()
 * /userinfo        POST    -> create()
 * /userinfo/{id}   GET     -> read()
 * /userinfo/{id}   PUT     -> update()
 * /userinfo/{id}   DELETE  -> delete()
 * /userinfo        DELETE  -> batchDelete()
 * </pre>
 * 
 * @author zhouych
 * @param <T>
 *            被操作的实体类型
 */
public abstract class BaseRestController<T> {
	
	/**
	 * 获取所有对象, 对象通过分页进行展示, 默认为展示第一页.
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public abstract String show(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "page", required = false) int page);

	/**
	 * 创建新的对象.
	 * 
	 * @param request
	 * @param response
	 * @param t
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public abstract String create(HttpServletRequest request,
			HttpServletResponse response, T t);
	
	/**
	 * 显示指定的对象.
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public abstract String read(HttpServletRequest request, HttpServletResponse response,
			@PathVariable Serializable id);
	
	/**
	 * 更新指定的对象.
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public abstract String update(HttpServletRequest request,
			HttpServletResponse response, @PathVariable Serializable id);
	
	/**
	 * 删除指定的对象.
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public abstract String delete(HttpServletRequest request,
			HttpServletResponse response, @PathVariable Serializable id);
	
	/**
	 * 批量删除指定的对象集.
	 * 
	 * @param request
	 * @param response
	 * @param items
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE)
	public abstract String batchDelete(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("items") Serializable[] items);
}
